/**
 * 分类接口
 *
 * @project sky-take-out_1
 * @author MECHREVO
 * @date 2023/12/3 17:18:43
 * @version 1.0
 */
package com.sky.controller.admin;

import com.sky.dto.CategoryDTO;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.entity.Category;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.CategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/admin/category")
@Slf4j
@Api(tags = "分类相关接口")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    /**
     * 分类查询
     * @param categoryPageQueryDTO
     * @return
     */
   @GetMapping("/page")
   @ApiOperation("分类查询")
    public Result page(CategoryPageQueryDTO categoryPageQueryDTO){
       log.info("分类查询：{}",categoryPageQueryDTO);
       PageResult pageResult=categoryService.page(categoryPageQueryDTO);
       return Result.success(pageResult);
    }

    @PostMapping
    @ApiOperation("新增分类")
    public Result add(@RequestBody CategoryDTO categoryDTO){
    categoryService.add(categoryDTO);
    return Result.success();
    }

    /**
     * 删除分类
     * @param id
     * @return
     */
    @DeleteMapping
    @ApiOperation("删除分类")
    public Result delete(Long id){
        log.info("删除分类：{}", id);
        categoryService.deleteById(id);
        return Result.success();
    }

    /**
     * 修改的分类
     * @param categoryDTO
     * @return
     */
    @PutMapping
    @ApiOperation("修改分类")
    public Result update(@RequestBody CategoryDTO categoryDTO){
        categoryService.update(categoryDTO);
        return Result.success();
    }

    /**
     * 状态的起售和关闭
     * @param id
     * @param status
     * @return
     */
    @PostMapping("/status/{status}")
    public Result statusor(Long id, @PathVariable Integer status) {
        categoryService.updateById(id, status);
        return Result.success();
    }
    @GetMapping("/list")
    public Result list(Integer type){
        List<Category>list =categoryService.list(type);
        return Result.success(list);
    }
}
